package com.jk.util;

import org.smart4j.framework.util.DateUtil;
import org.smart4j.framework.util.ReflectionUtil;

/**
 * Created by DELL on 2016/10/17.
 */
public class BaseDataUtil {

    /**
     * 设置程序基础数据
     * @param entity
     * @param operatorFlag
     * @param operatorID
     * @param <T>
     * @return
     */
    public  static <T> T setBaseDataForEntity(T entity, String operatorFlag, String operatorID) {
        // 新增
        if (operatorFlag.equals("1")) {
            ReflectionUtil.setField(entity,"deleteFlag","0");
            ReflectionUtil.setField(entity,"updateCount",1);
            ReflectionUtil.setField(entity,"createDate", DateUtil.getNowTime());
            ReflectionUtil.setField(entity,"createUserId",operatorID);
            ReflectionUtil.setField(entity,"updateDate",DateUtil.getNowTime());
            ReflectionUtil.setField(entity,"updateUserId",operatorID);
            ReflectionUtil.setField(entity,"updateFlag",operatorFlag);
        }
        // 修改
        else if (operatorFlag.equals("2")) {
            int updateCount = (int)ReflectionUtil.getField(entity,"updateCount") + 1;
            ReflectionUtil.setField(entity,"deleteFlag","0");
            ReflectionUtil.setField(entity,"updateCount",updateCount);
            ReflectionUtil.setField(entity,"updateDate",DateUtil.getNowTime());
            ReflectionUtil.setField(entity,"updateUserId",operatorID);
            ReflectionUtil.setField(entity,"updateFlag",operatorFlag);
        }
        // 删除
        else if (operatorFlag.equals("3")) {
            int updateCount = (int)ReflectionUtil.getField(entity,"updateCount") + 1;
            ReflectionUtil.setField(entity,"deleteFlag","1");
            ReflectionUtil.setField(entity,"updateCount",updateCount);
            ReflectionUtil.setField(entity,"updateDate",DateUtil.getNowTime());
            ReflectionUtil.setField(entity,"updateUserId",operatorID);
            ReflectionUtil.setField(entity,"updateFlag",operatorFlag);

        }
        return entity;
    }


    /**
     * 设置新增基础数据
     * @param entity
     * @param operatorID
     * @param <T>
     * @return
     */
    public  static <T> T setInsertBaseData(T entity, String operatorID) {
        return  BaseDataUtil.setBaseDataForEntity(entity,"1",operatorID);
    }

    /**
     * 设置更新基础数据
     * @param entity
     * @param operatorID
     * @param <T>
     * @return
     */
    public  static <T> T setUpdateBaseData(T entity, String operatorID) {
        return  BaseDataUtil.setBaseDataForEntity(entity,"2",operatorID);
    }

    /**
     * 设置删除基础数据
     * @param entity
     * @param operatorID
     * @param <T>
     * @return
     */
    public  static <T> T setDelBaseData(T entity, String operatorID) {
        return  BaseDataUtil.setBaseDataForEntity(entity,"3",operatorID);
    }




}
